﻿using DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Database = DAL.Database;

namespace BLL
{
    public class UserService
    {
        public static User Login(String username, String password)
        {
            try
            {
                using (var context = Database.GetContext())
                {

                    var user = from c in context.Users
                               where c.UserIdentity == username && c.UserPassword==password
                               select c;
                    return user.First();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return null;
        }

        public static bool IsLogin(Page page)
        {

            var b = page.Session["username"] == null
                || (String)page.Session["username"] == "";
            return !b;
        }

        public static void RequireLogin(Page page)
        {
            if (!page.IsPostBack)
            {
                if (!IsLogin(page))
                {
                    page.Response.Redirect("/Account/Login");
                }
            }
        }

        public static void Register(User user)
        {
            Database.Insert(user);
        }
    }
}